package com.lzm.ydpt.module.riding.activity;

import android.text.TextUtils;
import android.view.View;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.basemvp.boby.FormBodys;
import com.byt.framlib.commonutils.FormatUtil;
import com.byt.framlib.commonwidget.ClearableEditText;
import com.byt.framlib.commonwidget.NormalTitleBar;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.R;
import com.lzm.ydpt.mvp.contract.ContactUserContract;
import com.lzm.ydpt.mvp.presenterimpl.ContactUserPresenterImpl;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 紧急联系人
 */
public class ContactUserActivity extends BaseActivity<ContactUserPresenterImpl> implements ContactUserContract.View {
    @BindView(R.id.ntb_contact_user)
    NormalTitleBar ntb_contact_user;
    @BindView(R.id.edt_contact_user_name)
    ClearableEditText edt_contact_user_name;
    @BindView(R.id.edt_contact_user_phone)
    ClearableEditText edt_contact_user_phone;

    @Override
    public int getLayoutId() {
        return R.layout.activity_contact_user;
    }

    @Override
    public ContactUserPresenterImpl initPreData() {
        return new ContactUserPresenterImpl(this);
    }

    @Override
    public void initView() {
        setNtbTitle(ntb_contact_user, false);
        ntb_contact_user.setOnBackListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        if (Dapplication.getUserInfo() != null) {
//        ImageLoaderUtils.displayCircle(img_my_info_avater, Dapplication.getUserInfo().get);
            if (!TextUtils.isEmpty(Dapplication.getUserInfo().getEmergencyContactName())) {
                edt_contact_user_name.setText(Dapplication.getUserInfo().getEmergencyContactName());
            }
            if (!TextUtils.isEmpty(Dapplication.getUserInfo().getEmergencyContactPhone())) {
                edt_contact_user_phone.setText(Dapplication.getUserInfo().getEmergencyContactPhone());
            }

        }
    }

    @OnClick({R.id.tv_save_contact_user})
    public void onClick(View view) {
        if (AntiShake.check(view.getId())) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_save_contact_user:
                if (TextUtils.isEmpty(edt_contact_user_phone.getText().toString()) && TextUtils.isEmpty(edt_contact_user_name.getText().toString())) {
                    showShortToast("请填写需要紧急联系人或电话");
                    return;
                }
                if (!TextUtils.isEmpty(edt_contact_user_phone.getText().toString()) && edt_contact_user_phone.getText().toString().length() != 11) {
                    showShortToast("请输入11位的手机号");
                    return;
                }
                if (!TextUtils.isEmpty(edt_contact_user_phone.getText().toString()) && !FormatUtil.isMobileNO(edt_contact_user_phone.getText().toString())) {
                    showShortToast("请输入正确的手机号");
                    return;
                }
                startProgressDialog();
                postContactData();
                break;
        }
    }

    private void postContactData() {
        FormBodys.Builder builder = new FormBodys.Builder()
                .add("memberId", Dapplication.getUserToken());
        if (!TextUtils.isEmpty(edt_contact_user_name.getText().toString())) {
            builder.add("emergencyContactName", edt_contact_user_name.getText().toString());
        }
        if (!TextUtils.isEmpty(edt_contact_user_phone.getText().toString())) {
            builder.add("emergencyContactPhone", edt_contact_user_phone.getText().toString());
        }
        mPresenter.onContactUser(builder.build());
    }

    @Override
    public void onContactUser(String msg) {
        stopProgressDialog();
        showShortToast(msg);
        if (!TextUtils.isEmpty(edt_contact_user_name.getText().toString())) {
            Dapplication.getUserInfo().setEmergencyContactName(edt_contact_user_name.getText().toString());
        }
        if (!TextUtils.isEmpty(edt_contact_user_phone.getText().toString())) {
            Dapplication.getUserInfo().setEmergencyContactPhone(edt_contact_user_name.getText().toString());
        }
        finish();
    }

    @Override
    public void showMessage(String message, String method) {
        stopProgressDialog();
        showShortToast(message);
    }

    @Override
    public void showErrorView(String msg) {

    }
}